<template>
  <div class="app-container">
    <div style="display: flex; justify-content: space-between">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['attendance:group:add']"
            >新增考勤组</el-button
          >
        </el-col>
      </el-row>
      <!-- 全部考勤组表单搜索框-->
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item>
          <el-select
            v-model="queryParams.groupType"
            filterable
            placeholder="全部类型考勤组"
            style="width: 180px"
            @change="queryGroupTypeList(queryParams.groupType)"
          >
            <el-option
              v-for="dict in classSystemTypeOption"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.name"
            style="max-width: 200px; margin-left: 10px"
            placeholder="请输入考勤组名称"
            clearable
            @change="handleQuery"
          >
            <template #append>
              <el-button :icon="Search" />
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="postList"
      :default-sort="{ prop: 'name', order: 'descending' }"
    >
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="人数" align="center" prop="peopleCount" />
      <el-table-column label="类型">
        <template #default="scope">
          <span>{{ groupTypeOption(scope.row.groupType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            v-hasPermi="['attendance:group:edit']"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            v-hasPermi="['attendance:group:remove']"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Group">
import { ref, onMounted } from "vue";
import {
  attendanceListPost,
  attendanceDelPost,
} from "@/api/attendance/attendance.js";
import { Search } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
const { proxy } = getCurrentInstance();
const postList = ref([]);
const loading = ref(true);
const total = ref(0);
const router = useRouter();
// 全部考勤组选择框
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    groupType: undefined,
    name: undefined,
  },
});

const { queryParams } = toRefs(data);

onMounted(() => {
  getList();
});

const classSystemTypeOption = ref([
  {
    label: "固定班制",
    value: 1,
  },
  {
    label: "自由班制",
    value: 2,
  },
]);

/**
 * 分页列表类型转换
 * @param val
 */
function groupTypeOption(val) {
  if (val == 1) {
    return "固定班制";
  } else if (val == 2) {
    return "自由班制";
  } else {
    return " ";
  }
}

/** 这个是列表数据  */
function getList() {
  loading.value = true;
  attendanceListPost(queryParams.value).then((response) => {
    postList.value = response.rows || [];
    total.value = Number(response.total);
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.name = queryParams.value.name;
  queryParams.value.pageNum = 1;
  getList();
}

/** 新增按钮操作 */
function handleAdd() {
  router.push("addattendance");
}

/**修改按钮操作 */
function handleUpdate(row) {
  router.push({
    path: "addattendance",
    query: { rowId: row.id },
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const groupId = row.id;
  proxy.$modal
    .confirm('是否确认删除考勤组为"' + row.name + '"的数据项？')
    .then(function () {
      return attendanceDelPost(groupId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/**
 * 根据考勤类型查询列表
 */
const queryGroupTypeList = (val) => {
  queryParams.value.pageNum = 1;
  queryParams.value.groupType = val;
  getList();
};
</script>
